package com.ymatou.iapi.userservice.testcase;

import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.UpdateTradingPwdBean;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.UpdateTradingPwdCall;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;

/**
 * 更新交易密码Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UpdateTradingPwd {
	private static UpdateTradingPwdBean updatetradingpwdBean;
	private static UpdateTradingPwdCall updatetradingpwdCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新交易密码Test");
	}

	@Before
	public void caseUp() {
		updatetradingpwdBean = new UpdateTradingPwdBean();
		updatetradingpwdCall = new UpdateTradingPwdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatetradingpwdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateTradingPwd_001() {
		Logger.start(true, "更新交易密码,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);

			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword(null);
			updatetradingpwdBean.setNewPassword("123abc");
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("true", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");

			Logger.comment("验证Userinfo的TradingPassword");
			JUserWapper ju = new JUserWapper();
			List<Userinfo> lui = ju.selectUserinfoByUid(uid);
			Userinfo ui = lui.get(0);
			Logger.verifyEquals("A86E53F75B7C47F46D537B4BBA698904", ui.getTradingPassword(), "验证交易密码是否更新成功");
			//支付，验证交易密码是否更新
			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword("123abc");
			updatetradingpwdBean.setNewPassword("abc123");
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("true", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");

			Logger.comment("验证Userinfo的TradingPassword");
			List<Userinfo> lui2 = ju.selectUserinfoByUid(uid);
			Userinfo ui2 = lui2.get(0);
			Logger.verifyEquals("54E12B6B392FD98136162FE3F7343AD5", ui2.getTradingPassword(), "验证交易密码是否更新成功");
			


		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateTradingPwd_002() {
		Logger.start(true, "修改交易密码，交易密码与登陆密码一样，失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);

			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword(null);
			updatetradingpwdBean.setNewPassword(JUserCallService.defaultLoginPwd);
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("false", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("300005", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("交易密码与登录密码相同", updatetradingpwdCall.getString("LastErrorMessage"),
					"验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateTradingPwd_003() {
		Logger.start(true, "交易密码为空，修改失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);

			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword(null);
			updatetradingpwdBean.setNewPassword(null);
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("false", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("100", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("NewPassword 不能为空", updatetradingpwdCall.getString("LastErrorMessage"),
					"验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateTradingPwd_004() {
		Logger.start(true, "更新交易密码,密码DES加密,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);

			updatetradingpwdBean.setPasswordType(1);
			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword(null);
			String pwd = DesUtil.encrypt("123abc",DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
			String npwd = DesUtil.encrypt("abc123",DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
			updatetradingpwdBean.setNewPassword(pwd);
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("true", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");

			Logger.comment("验证Userinfo的TradingPassword");
			JUserWapper ju = new JUserWapper();
			List<Userinfo> lui = ju.selectUserinfoByUid(uid);
			Userinfo ui = lui.get(0);
			Logger.verifyEquals("A86E53F75B7C47F46D537B4BBA698904", ui.getTradingPassword(), "验证交易密码是否更新成功");
			//支付，验证交易密码是否更新
			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword(pwd);
			updatetradingpwdBean.setNewPassword(npwd);
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("true", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");

			Logger.comment("验证Userinfo的TradingPassword");
			List<Userinfo> lui2 = ju.selectUserinfoByUid(uid);
			Userinfo ui2 = lui2.get(0);
			Logger.verifyEquals("54E12B6B392FD98136162FE3F7343AD5", ui2.getTradingPassword(), "验证交易密码是否更新成功");
			


		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateTradingPwd_005() {
		Logger.start(true, "更新交易密码,密码RSA加密,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);

			updatetradingpwdBean.setPasswordType(2);
			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword(null);
			String pwd =JUserCallService.getRsaPwd("trading123");
			String npwd =JUserCallService.getRsaPwd("trading321");
//			String pwd = DesUtil.encrypt("123abc",DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
//			String npwd = DesUtil.encrypt("abc123",DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
			updatetradingpwdBean.setNewPassword(pwd);
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("true", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");

			Logger.comment("验证Userinfo的TradingPassword");
			JUserWapper ju = new JUserWapper();
			List<Userinfo> lui = ju.selectUserinfoByUid(uid);
			Userinfo ui = lui.get(0);
			String old=ui.getTradingPassword();
//			Logger.verifyEquals("A86E53F75B7C47F46D537B4BBA698904", ui.getTradingPassword(), "验证交易密码是否更新成功");
			//支付，验证交易密码是否更新
			updatetradingpwdBean.setUserId(uid);
			updatetradingpwdBean.setOldPassword(pwd);
			updatetradingpwdBean.setNewPassword(npwd);
			// ModifyTradingPassword 或 ModifyLoginPassword
			updatetradingpwdBean.setPasActionType("ModifyTradingPassword");
			updatetradingpwdBean.setLoginMobile(cur.getMobile());
			// ByUserId 或 ByMobile
			updatetradingpwdBean.setAType("ByUserId");
			updatetradingpwdCall.setData(updatetradingpwdBean);
			updatetradingpwdCall.callService();

			Logger.verifyEquals("true", updatetradingpwdCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", updatetradingpwdCall.getString("ResponseCode"), "验证ResponseCode");

			Logger.comment("验证Userinfo的TradingPassword");
			List<Userinfo> lui2 = ju.selectUserinfoByUid(uid);
			Userinfo ui2 = lui2.get(0);
			Logger.verifyNotEquals(old, ui2.getTradingPassword(), "验证交易密码是否更新成功");
			


		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}